package com.helper.dream.a361phonehelper.fragment.detail;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.helper.dream.a361phonehelper.R;
import com.helper.dream.a361phonehelper.adapter.DetailRecyclerAdapter;
import com.helper.dream.a361phonehelper.bean.NewsBean;
import com.helper.dream.a361phonehelper.fragment.RedianFragment;
import com.helper.dream.a361phonehelper.utils.ApiManager;
import com.helper.dream.a361phonehelper.utils.DownString;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import in.srain.cube.views.ptr.PtrFrameLayout;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * Created by Administrator on 2016/12/19.
 */

public class ImageAndTextFragment extends Fragment {
    private PtrFrameLayout ptrFrameLayout;
    private RecyclerView recyclerView;
    private List<NewsBean.DataBean.ResBean> list;
    private DetailRecyclerAdapter adapter;
    private String c;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            adapter.notifyDataSetChanged();
        }
    };
    View view;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        aboutData();

    }

    private void aboutData() {
        list = new ArrayList<>();
        Bundle b = getArguments();
        if (b != null) {
            c = b.getString(RedianFragment.CLASS_KIND_KEY);
        } else {
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.detail_fragment_layout, container, false);
        initViews(view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        loadData3();
    }

    private void loadData3() {
        String url = null;
        if ("video".equals(c)) {
            url = ApiManager.BASE_URL_SJ;
        } else {
            url = ApiManager.BASE_URL_LOOK;
        }
        url = url + ApiManager.RECOMMON_URL + "&c=" + c;
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .build();
        okhttp3.Call call = client.newCall(request);
        call.enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {
                TextView tv = new TextView(getActivity());
                tv.setText("加载失败~~~~~~~~~~");
            }

            @Override
            public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
                String msg = response.body().string();
                try {
                    JSONObject jo = new JSONObject(msg);
                    int errno = jo.getInt("errno");
                    if (errno == 0) {
                        NewsBean nb = new Gson().fromJson(msg, NewsBean.class);
                        List<NewsBean.DataBean.ResBean> res = nb.getData().getRes();
                        if (list == null) {
                            list = new ArrayList<>();
                        }
                        list.clear();
                        list.addAll(res);
                        for (final NewsBean.DataBean.ResBean a : list) {
                            String url2 = a.getVideoUrl();
//                    L.d(url2);
                            //二层
                            if (url2 != null) {
                                DownString.loadString(url2, new DownString.CallBack() {
                                    @Override
                                    public void htmlString(String msg) {
                                        try {
                                            JSONObject jo = new JSONObject(msg);
                                            jo = jo.getJSONObject("data");
                                            String urlStr = jo.getString("playLink");
                                            DownString.loadString(urlStr, new DownString.CallBack() {
                                                @Override
                                                public void htmlString(String msg) {
                                                    try {
                                                        JSONObject jo = new JSONObject(msg);
                                                        jo = jo.getJSONObject("data");
                                                        String videoUrl = jo.getString("url");
                                                        a.playerUrl = videoUrl;
//                                            L.d("videoUrl:" + list.get(0).aboutRadioUrl);
                                                        handler.sendEmptyMessage(0x002);
                                                    } catch (JSONException e) {
                                                        e.printStackTrace();
                                                    }
                                                }
                                            });

                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                            }
                            handler.sendEmptyMessage(0x001);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }


    private void initViews(View view) {
        ptrFrameLayout = (PtrFrameLayout) view.findViewById(R.id.ptr_frame_layout);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        LinearLayoutManager l = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(l);
        adapter = new DetailRecyclerAdapter(getActivity(), list);
        recyclerView.setAdapter(adapter);
    }

}
